const Router = require('koa-router')
const { verify: registerVerify } = require('../middlewares/register/validator')
const { verify: loginVerify } = require('../middlewares/login/validator')
const { SuccessException } = require('../../../../core/http-exception')
const AuthService = require('../../../services/auth')

const router = new Router({
  prefix: '/v1/user',
})

router.post('/register', registerVerify, async (ctx) => {
  // 注册
  await AuthService.Register(ctx.v)
  // 返回响应
  throw new SuccessException({ msg: '注册成功' })
})
router.post('/login', loginVerify, async (ctx) => {
  // 获取token
  const token = await AuthService.Login(ctx.v)
  // 返回响应
  throw new SuccessException({ msg: '登录成功', data: { token } })
})

module.exports = router
